package servlets;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import pojo.EDeviceType;
import pojo.User;

import utils.db.impl.DeviceAdd;

/**
 * Servlet implementation class AddDeviceServlet
 */
@WebServlet("/AddDeviceServlet")
public class AddDeviceServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int userId = Integer.parseInt(request.getParameter("userId"));
		String type = request.getParameter("deviceType");
		String desc = request.getParameter("desc");
		HttpSession session = request.getSession(true);
		User currentUser = (User) session.getAttribute("user");
		int currentUserId = currentUser.getId();
		
		if(userId!=0 && type!="0" && desc!=""){
			DeviceAdd add=new DeviceAdd();
			add.addDevice(desc, EDeviceType.valueOf(type), userId, currentUserId);
			session.setAttribute("warning", "Urządzenie zostało dodane!");
			response.setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY);
			response.setHeader("Location", "index.jsp?page=addDevice#pags");
		}else{
			session.setAttribute("warning", "Problem przy dodaniu urządzenia!");
			response.setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY);
			response.setHeader("Location", "index.jsp?page=addDevice#pags");
		}
	}

}
